package studio.wetrack.dinner.domain;

import lombok.Data;
import org.springframework.beans.BeanUtils;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;

/**
 * Created by zhanghong on 2017/8/12.
 */
@Data
@Entity(name = "dish")
public class Dish {

    @Id
    long id;
    int price;
    String name;
    int weight;
    String type;
    int share;


    //从菜模版中创建一个实例菜
    public Dish getOneInstance(){
        Dish instance = new Dish();
        BeanUtils.copyProperties(this, instance);
        //一开始，一口也没吃过
        instance.remainShare = instance.share;
        return instance;
    }

    //以下是dish作为实例菜的属性和方法
    @Transient
    int remainShare;

    public synchronized boolean takeOneShare(){
        if(remainShare > 0){
            remainShare--;
            return true;
        }
        return false;
    }


    @Override
    public String toString() {
        return "Dish{" +
                "id=" + id +
                ", price=" + price +
                ", name='" + name + '\'' +
                ", weight=" + weight +
                ", type='" + type + '\'' +
                ", share=" + share +
                '}';
    }
}
